﻿using System.Collections.Generic;
using Application.DTO;
using DomainModel.Support;
using Infrastructure.DC;
using System.Linq;

namespace Application.Task
{
    public class ChangeCustomerDetails
    {
        private CustomerInfo _customerInfo;
        private ICustomersDao _customersDao;
        private  List<BrokenBusinessRuleInfo> _brokenRules = new List<BrokenBusinessRuleInfo>();
        public IEnumerable<BrokenBusinessRuleInfo> BrokenRules { get { return _brokenRules;}}
        
        public bool Succes { get; set; }

        public ChangeCustomerDetails(CustomerInfo customerInfo):this(customerInfo, new CustomersDao())
        {
            
        }
        public ChangeCustomerDetails(CustomerInfo customerInfo, ICustomersDao customersDao )
        {
            _customersDao = customersDao;
            _customerInfo = customerInfo;
        }
        public void Execute()
        {
            var customer = _customersDao.RetrieveById(_customerInfo.CustomerId);
            if(customer!=null)
            {
                customer.FirstName = _customerInfo.FirstName;
                customer.LastName = _customerInfo.LastName;

                List<BrokenBusinessRule> brokenRules = customer.GetBrokenRules();
                if (brokenRules.Count == 0)
                {
                    _customersDao.Update(customer);
                    Succes = true;
                }
                else
                {
                    _brokenRules = new BrokenBusinessRuleInfoMapper().From(brokenRules).ToList();
                    Succes = false;
                }
            }
            else
            {
               
                _brokenRules.Add(new BrokenBusinessRuleInfo() {ErrorMessage = "Not found"});
                Succes = false;
            }
        }
    }
}
